﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.BaseInfo.DTO.Department;
using AnQinFrameWorkAPI.Entity.BaseInfo.Entity;
using AnQinFrameWorkAPI.Entity.BaseInfo.View.Department;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.BaseInfo;

/// <summary>
///     部门业务层
/// </summary>
public class DepartmentService
{
    #region 获取部门列表

    public async Task<RequestResponseModel> GetDepartmentList(DepartmentSearchDTO departmentSearchDTO)
    {
        //筛选
        var query = _context.Department
                .ProjectTo<DepartmentView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(
                    a => a.Code.Contains(departmentSearchDTO.Department) ||
                         a.Name.Contains(departmentSearchDTO.Department),
                    !string.IsNullOrEmpty(departmentSearchDTO.Department))
                .WhereIF(a => a.CreateTime > departmentSearchDTO.StartTime, departmentSearchDTO.StartTime != null)
                .WhereIF(a => a.CreateTime < departmentSearchDTO.EndTime, departmentSearchDTO.EndTime != null)
            ;

        //返回列表
        var DepartmentList = await query
            .OrderBy(a => a.Order)
            .Page(departmentSearchDTO.PageIndex, departmentSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DepartmentView>(DepartmentList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增部门

    public async Task<RequestResponseModel> AddDepartment(DepartmentAddDTO departmentAddDTO)
    {
        //生成新数据
        var department = _mapper.Map<DepartmentModel>(departmentAddDTO);

        department.Id = Guid.NewGuid();
        department.CreateTime = DateTime.Now;

        //新增数据
        _context.Department.Add(department);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(department.Id, "新增成功");
    }

    #endregion

    #region 更新部门

    public async Task<RequestResponseModel> PutDepartment(DepartmentUpdateDTO departmentUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.Department, departmentUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("菜单Id无效");
        ;

        //编辑数据
        var department = _mapper.Map(departmentUpdateDTO, query);

        //更新表
        _context.Department.Update(department);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(departmentUpdateDTO.Id);
    }

    #endregion

    #region 删除部门

    public async Task<RequestResponseModel> DeleteDepartment(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.Department, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("部门Id无效");

        var staffList = _context.Staff.FirstOrDefault(a => a.DepartmentId == query.Id);
        if (staffList != null) return RequestResponse.BadRequest("该部门下有员工，无法删除！");

        //删除操作
        _context.Department.Remove(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion

    #region 部门侧边栏

    public async Task<RequestResponseModel> GetDepartmentListAside(DepartmentAsideSearchDTO departmentAsideSearchDTO)
    {
        //筛选
        var query = _context.Department
                .ProjectTo<DepartmentView>(_mapper.ConfigurationProvider)
                .AsNoTracking()
                .WhereIF(
                    a => a.Code.Contains(departmentAsideSearchDTO.Department) ||
                         a.Name.Contains(departmentAsideSearchDTO.Department),
                    !string.IsNullOrEmpty(departmentAsideSearchDTO.Department))
            ;

        //返回列表
        var departmentList = await query
            .OrderBy(a => a.Order)
            .ToListAsync();

        //返回数据
        return RequestResponse.OK(departmentList, "查询成功");
    }

    #endregion

    #region 初始化

    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public DepartmentService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #endregion
}